home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / Thumbnail Test / ShrinkToBW.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.5 KB  |  119 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        ShrinkToBW.c
  3.  
  4.     Contains:    this file contains a routine to illustrate the use of copybits to generate thumbnail images
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/14/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #include "CarbonPrefix.h"
  24. #include <QDOffscreen.h>
  25. #include <PictUtils.h>
  26. #include <Windows.h>
  27.  
  28. extern RGBColor kRGBBlack ;
  29. extern RGBColor kRGBWhite ;
  30. extern Point gStaggerPos ;
  31.  
  32. void ShrinkToBWPict( WindowPtr theWindow );
  33.  
  34.  
  35. #define    SCALEFACTOR        15    // percent
  36.  
  37. void ShrinkToBWPict( WindowPtr theWindow )
  38. {
  39.     Rect        theBounds, tempRect, tempRect2 ;
  40.     GWorldPtr    theOldWorld, theNewWorld ;
  41.     OSErr        theErr ;
  42.     CGrafPtr    savedPort ;
  43.     WindowPtr    newWindow ;
  44.     GDHandle    oldDevice ;
  45.     
  46.     PaletteHandle    thePictPalette = nil ;
  47.     CTabHandle        thePictCTab = nil ;
  48.     
  49.     float        scaleFactor = SCALEFACTOR / 100.0 ;
  50.     
  51.     
  52.     // get the GWorld from the window refcon
  53.     theOldWorld = (GWorldPtr)GetWRefCon ( theWindow );
  54.     
  55.     // get the bounds of the window
  56.     //theBounds = theOldWorld->portRect ;
  57.     GetPortBounds ( theOldWorld, &theBounds );
  58.     
  59.     // apply the scaling factor
  60.     theBounds.top = scaleFactor * theBounds.top ;
  61.     theBounds.left = scaleFactor * theBounds.left ;
  62.     theBounds.bottom = scaleFactor * theBounds.bottom ;
  63.     theBounds.right = scaleFactor * theBounds.right ;
  64.     
  65.     // make a new one-bit gworld to hold the shrunken image
  66.     theErr = NewGWorld( &theNewWorld, 1, &theBounds, nil, nil, 0L ) ;
  67.     
  68.     if( theErr != noErr ) 
  69.         return ;
  70.     
  71.     // save the world
  72.     GetGWorld( &savedPort, &oldDevice ) ;
  73.     SetGWorld( theNewWorld, nil ) ;
  74.     
  75.     
  76.     RGBForeColor( &kRGBBlack ) ;        // ensure the fg and bg colors are 
  77.     RGBBackColor( &kRGBWhite ) ;        // as anticipated
  78.     EraseRect( &theBounds ) ;            // clear the area for the pict
  79.     PenMode( srcCopy ) ;                // ensure the t/f mode is as expected
  80.  
  81.  
  82.     // render the image into the offscreen buffer
  83.     
  84.     /*CopyBits( &((GrafPtr)theOldWorld)->portBits,
  85.               &((GrafPtr)theNewWorld)->portBits,
  86.               &theOldWorld->portRect,
  87.               &theNewWorld->portRect,
  88.               patCopy | ditherCopy,
  89.               nil ) ;*/
  90.     CopyBits(     GetPortBitMapForCopyBits(theOldWorld),
  91.                 GetPortBitMapForCopyBits(theNewWorld),
  92.                 GetPortBounds(theOldWorld, &tempRect),
  93.                 GetPortBounds(theNewWorld, &tempRect2),
  94.                 patCopy | ditherCopy,
  95.                 nil );
  96.     
  97.     SetGWorld( savedPort, oldDevice ) ;
  98.     
  99.     // create the window
  100.     OffsetRect( &theBounds, gStaggerPos.h, gStaggerPos.v) ;
  101.     gStaggerPos.h += 16 ;
  102.     gStaggerPos.v += 16 ;        // heh - should roll these around, but you wont 
  103.                                 // create more than a couple of windows, will you  :-)
  104.                                  
  105.     newWindow  = NewCWindow( nil, &theBounds, "\pplayTime", true, 
  106.                                 documentProc, (WindowPtr)-1, true, (long)theNewWorld );    
  107.     
  108.     // make sure it is visible
  109.     ShowWindow( newWindow ) ;
  110.     
  111.     SetGWorld( (CGrafPtr)newWindow, nil ) ;
  112.     
  113.     // invalidate the content region of the window - we don't do any drawing to it here.
  114.     // Not suppoted in carbon, use InvalWindowRect
  115.     //InvalRect ( &theBounds );
  116.     InvalWindowRect ( newWindow, &theBounds ); 
  117.     
  118.     SetGWorld( savedPort, oldDevice ) ;
  119. }